/*
 * config_dialog.cpp
 *
 *  Created on: 11.08.2013
 *      Author: SpirTanol
 */

#include "ui/config_dialog.h"

// Qt
#include <QCheckBox>
#include <QGroupBox>
#include <QBoxLayout>
#include <QPushButton>
#include <QRadioButton>
#include <QSpinBox>
#include <QLabel>

ConfigDialog::ConfigDialog(bool autoConnect, ScaleType scaleType, uint interval)
{
	setWindowTitle(trUtf8("Параметры"));
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);


	QBoxLayout* box = new QBoxLayout(QBoxLayout::TopToBottom, this);

	oCheckAutoConnect = new QCheckBox(trUtf8("Автоматическое подключение при запуске программы"));
	oCheckAutoConnect->setChecked(autoConnect);
	box->addWidget(oCheckAutoConnect);

	QGroupBox *groupScaleType = new QGroupBox(trUtf8("Масштаб оси токов токо-временной характеристики"));
	QBoxLayout *box2 = new QBoxLayout(QBoxLayout::TopToBottom, groupScaleType);

	oRadioScaleType[ST_LINEAR] = new QRadioButton(trUtf8("Линейный"));
	box2->addWidget(oRadioScaleType[ST_LINEAR]);

	oRadioScaleType[ST_LOGO] = new QRadioButton(trUtf8("Логарифмический"));
	box2->addWidget(oRadioScaleType[ST_LOGO]);

	oRadioScaleType[scaleType]->setChecked(true);

	box->addWidget(groupScaleType);

	box->addWidget(new QLabel(trUtf8("Интервал графиков")));
	QBoxLayout *intervalBox = new QBoxLayout(QBoxLayout::RightToLeft);
	box->addLayout(intervalBox);

	oSpinMinute = new QSpinBox();
	oSpinMinute->setRange(0, 59);
	oSpinMinute->setSuffix(trUtf8("мин."));
	oSpinMinute->setValue((interval % 3600) / 60);
	intervalBox->addWidget(oSpinMinute);

	oSpinHour = new QSpinBox();
	oSpinHour->setRange(0, 24);
	oSpinHour->setSuffix(trUtf8("ч."));
	oSpinHour->setValue(interval / 3600);
	intervalBox->addWidget(oSpinHour);

	QBoxLayout* box3 = new QBoxLayout(QBoxLayout::LeftToRight);
	box->addLayout(box3);

	QPushButton *btn = new QPushButton(trUtf8("ОК"));
	box3->addWidget(btn);
	connect(btn, SIGNAL(clicked()), this, SLOT(accept()));

	btn = new QPushButton(trUtf8("Отмена"));
	box3->addWidget(btn);
	connect(btn, SIGNAL(clicked()), this, SLOT(reject()));

	setFixedSize(320, 210);
}

bool ConfigDialog::autoConnect() const
{
	return oCheckAutoConnect->isChecked();
}

ScaleType ConfigDialog::scaleType() const
{
	if (oRadioScaleType[ST_LINEAR]->isChecked())
		return ST_LINEAR;
	return ST_LOGO;
}

uint ConfigDialog::interval() const
{
	return oSpinMinute->value() * 60 + oSpinHour->value() * 3600;
}
